﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace TextMIS
{
    public partial class frmBrowseItem : Form
    {
        public frmBrowseItem()
        {
            InitializeComponent();
        }
        public static string userName;
        public static string userRight;
        public static string strSql;
        public static string regID;
        public static string itemNo;
        public static string itemName;
        SqlConnection conn;
        SqlCommand com;
        SqlDataAdapter da;
        SqlDataReader dr;

        private void frmBrowseItem_Load(object sender, EventArgs e)
        {
            userName = frmLogin.userName;
            userRight = frmLogin.userRight;
            conn = new
SqlConnection(frmLogin.SQLstr);
            fillTree();
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.View = View.Details;
            listView1.Scrollable = true;
            listView1.MultiSelect = false;
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;

            listView2.GridLines = true;
            listView2.FullRowSelect = true;
            listView2.View = View.Details;
            listView2.Scrollable = true;
            listView2.MultiSelect = false;
            listView2.HeaderStyle = ColumnHeaderStyle.Nonclickable;

            da = new SqlDataAdapter("select BranchNo,BranchName from BranchInfo ", conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "BranchInfo");

            conn.Close();

            this.tsbBranch.ComboBox.DataSource = ds.Tables[0];
            this.tsbBranch.ComboBox.DisplayMember = "BranchName";
            this.tsbBranch.ComboBox.ValueMember = "BranchNo";
            if (userRight == "普通用户")
            {

                this.ShowItem();
                this.PredictSum();
            }
            if (userRight == "系统管理员")
            {
                tsbOK.Enabled = false;
                tsbDelete.Enabled = false;
                this.ShowItem();
            }
        }

        public void GetRegId()
        {
            strSql = "select * from PatientInfo  where Username = '" + userName + "'";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            dr = com.ExecuteReader();

            if (dr.Read())
            {
                regID = dr.GetValue(0).ToString();
            }
            conn.Close();

        }

        public void LoadTreeS(TreeNode tn, string BranchName)
        {
            da = new SqlDataAdapter("select ItemName from ItemInfo where BranchName = '" + BranchName + "'", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode tns = new TreeNode();
                tns.Text = dr["ItemName"].ToString();
                tn.Nodes.Add(tns);
            }
        }

        private void fillTree()
        {
            this.treeView1.Nodes.Clear();
            DataTable dt = new DataTable();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da = new SqlDataAdapter("select BranchName from BranchInfo", conn);
            da.Fill(dt);
            conn.Close();
            TreeNode tnMain = new TreeNode("医院");
            for (int iRows = 0; iRows < dt.Rows.Count; iRows++)
            {
                TreeNode tnChild = new TreeNode(dt.Rows[iRows][0].ToString());
                LoadTreeS(tnChild, dt.Rows[iRows][0].ToString());
                tnMain.Nodes.Add(tnChild);

            }
            this.treeView1.Nodes.Add(tnMain);
            this.treeView1.ExpandAll();
            this.treeView1.SelectedNode = tnMain;
        }

        private void SetupHeaderText()
        {
            listView1.Columns.Add("项目编号", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("项目名称", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("所属科室", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("项目价格", 120, HorizontalAlignment.Left);
            listView1.Columns.Add("备注", 200, HorizontalAlignment.Left);
        }

        private void HeaderList2()
        {
            listView2.Columns.Add("注册编号", 100, HorizontalAlignment.Left);
            listView2.Columns.Add("用户姓名", 100, HorizontalAlignment.Left);
            listView2.Columns.Add("体检部门", 100, HorizontalAlignment.Left);
            listView2.Columns.Add("体检项目", 100, HorizontalAlignment.Left);
            listView2.Columns.Add("项目价格", 120, HorizontalAlignment.Left);

        }

        private void PredictSum()
        {
            int sum = 0;
            strSql = "select sum(ItemPrice) from viewPI_tab where Username = '" + userName + "'";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            dr = com.ExecuteReader();
            if (dr.Read())
            {
                sum = Convert.ToInt32(dr[0].ToString());
            }
            conn.Close();
            lblSum.Text = "预计共花费 " + sum.ToString() + " 元";
        }

        private void ShowItem()
        {
            try
            {
                GetRegId();
                listView2.Clear();
                da = new SqlDataAdapter("select RegisterID, Pname,BranchName ,ItemName, ItemPrice from viewPI_tab where RegisterID = '" + regID + "'", conn);

                DataTable dt = new DataTable();
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int count = da.Fill(dt);
                conn.Close();

                HeaderList2();
                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Clear();
                    lvi.SubItems[0].Text = dr[0].ToString();
                    lvi.SubItems.Add(dr[1].ToString());
                    lvi.SubItems.Add(dr[2].ToString());
                    lvi.SubItems.Add(dr[3].ToString());
                    lvi.SubItems.Add(dr[4].ToString());
                    listView2.Items.Add(lvi);      
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ShowInfo(string NodeText)
        {

            try
            {
                listView1.Clear();
                da = new SqlDataAdapter("select ItemNo, ItemName, BranchName, ItemPrice, ItemDetails from ItemInfo where ItemName = '" + NodeText + "'", conn);
                if (NodeText == "医院")
                {
                    da = new SqlDataAdapter("select ItemNo, ItemName, BranchName, ItemPrice, ItemDetails from ItemInfo", conn);
                }
                else
                {
                    char[] ch = NodeText.ToCharArray();
                    for (int i = 0; i < ch.Length; i++)
                    {
                        if (ch[i] == '科')
                        {
                            da = new SqlDataAdapter("select ItemNo, ItemName, BranchName, ItemPrice, ItemDetails from ItemInfo where BranchName = '" + NodeText + "'", conn);
                            break;
                        }
                    }
                }
                DataTable dt = new DataTable();
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int count = da.Fill(dt);
                conn.Close();
                lblItemNum.Text = "共有" + count.ToString() + "个体检项目";
                SetupHeaderText();
                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Clear();
                    lvi.SubItems[0].Text = dr[0].ToString();
                    lvi.SubItems.Add(dr[1].ToString());
                    lvi.SubItems.Add(dr[2].ToString());
                    lvi.SubItems.Add(dr[3].ToString());
                    lvi.SubItems.Add(dr[4].ToString());
                    listView1.Items.Add(lvi);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string nodeText = e.Node.Text.ToString();
            this.ShowInfo(nodeText);
        }

        private void tsbBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select ItemNo,ItemName from ItemInfo where BranchName='" + tsbBranch.Text + "'", conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "ItemInfo");

            conn.Close();

            this.tsbItem.ComboBox.DataSource = ds.Tables[0];
            this.tsbItem.ComboBox.DisplayMember = "ItemName";
            this.tsbItem.ComboBox.ValueMember = "ItemNo";
        }


        public void GetItemNo()
        {
            strSql = "select * from ItemInfo  where ItemName = '" + tsbItem.Text.Trim() + "'";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            dr = com.ExecuteReader();

            if (dr.Read())
            {
                itemNo = dr.GetValue(0).ToString();
            }
            conn.Close();
        }

        private void tsbOK_Click(object sender, EventArgs e)
        {
            GetItemNo();
            com = new SqlCommand("insert into PI_tab(Username,ItemNo,PrePaid)values('" + userName +
"','" + itemNo + "','0')", conn);

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("恭喜你！添加成功！");
            this.ShowItem();
            this.PredictSum();
        }

        public void GetItemName()
        {
            itemName = listView2.SelectedItems[0].SubItems[3].Text;
            strSql = "select * from ItemInfo  where ItemName = '" + itemName + "'";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            dr = com.ExecuteReader();

            if (dr.Read())
            {
                itemNo = dr.GetValue(0).ToString();
            }
            conn.Close();

        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            GetItemName();
            com = new SqlCommand("delete from PI_tab where Username = '" + userName + "'and ItemNo = '" + itemNo + "'", conn);

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("删除成功！");
            this.ShowItem();
            this.PredictSum();
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
